<?php

namespace Admin\Controllers;

use Phalcon\Mvc\Controller;

class BaseController extends Controller {

    protected $requestTime;
    protected $responseTime;

    public function initialize() {
        $this->logger->write_log(" ", "info");
        $this->logger->write_log(" ", "info");
        $this->logger->write_log(" ", "info");
        $this->logger->write_log(" ", "info");
        $this->requestTime = ceil(microtime(true) * 1000);
        $this->logger->write_log("requestTime :" . $this->requestTime, "info");
        $url = 'https://' . $_SERVER['SERVER_NAME'] . ':' . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
        //日志记录
        $headers = array();
        foreach ($_SERVER as $name => $value) {
            if (substr($name, 0, 5) == 'HTTP_') {
                $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
            }
        }
        $headersStr = "";
        foreach ($headers as $key => $value) {
            $headersStr .= $key . "=>" . $value . "\n";
        }
        $this->logger->write_log(
                "Url: " . $url . "\n" .
                "HttpHeaders: " . $headersStr . "\n"
                , 'info');



        $keyParams = $this->view->getParams();
        $getParams = "";
        foreach ($keyParams as $key => $value) {
            $getParams .= $key . "=>" . $value . "\n";
        }
        $postParams = "";
        foreach ($_POST as $key => $value) {
            $postParams .= $key . "=>" . $value . "\n";
        }
        $this->logger->write_log("\n" .
                "params: " . $getParams . "\n" .
                "postParams: " . $postParams . "\n"
                , 'info');
    }

    /**
     * 返回Api Json数据
     * @param $status
     * @param $data
     * @return mixed
     */
    protected function ajaxReturn($data, $info = '', $status = 200, $type = 'JSON') {
        $arr['data'] = $data;
        $arr['status'] = $status;
        $arr['info'] = $info;
        //记录接口返回信息
        if ($status != -1) {
            @$this->logger->write_log("返回结果:" . var_export($arr, true), "info");
            $this->logger->write_log("responseTime :" . $this->responseTime, "info");
            $this->logger->write_log("startTime :" . ($this->responseTime - $this->requestTime) . "毫秒", "info");
        }
        switch (strtoupper($type)) {
            case 'JSON' :
                // 返回JSON数据格式到客户端 包含状态信息
                header('Content-Type:application/json; charset=utf-8');
                exit(json_encode($arr));
            case 'XML' :
                // 返回xml格式数据
                header('Content-Type:text/xml; charset=utf-8');
                exit(xml_encode($arr));
        }
    }

    static public function appException($e) {
        //处理一下404错误的错误名称
        switch ($e->getCode()) {
            case \Phalcon\Mvc\Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
            case \Phalcon\Mvc\Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
                $logger = \Admin\Core\Logger::getInstance("empty");
                break;
            default :
                $logger = \Admin\Core\Logger::getInstance(__CONTROLLER_NAME__);
                break;
        }
        $log = " 
        " . "错误file: " . $e->getFile() . "
        " . "错误line: " . $e->getLine() . "
        " . "错误code: " . $e->getCode() . "
        " . "错误msg:  " . $e->getMessage() . "
        " . "错误trace: " . $e->getTraceAsString();
        $logger->write_log($log, "error");
        self::ajaxReturn(array(), $e->getMessage(), "-1");
    }

}
